package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.servercenter.TitleInfo;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TitleDaoImpl extends TempDao {

	public Map<Integer, TitleInfo> getTitleInfoMap() {
		ResultSet rs = null;
		CallableStatement call = null;
		String sql = "select * from t_p_title";
		Map<Integer, TitleInfo> dataMap = new ConcurrentHashMap<>();
		try {
			getDb().setConnName(DbNameStrategy.getConfigDb());
			call = getDb().getConnection().prepareCall(sql);
			rs = call.executeQuery();
			while (rs.next()) {
				TitleInfo data = new TitleInfo();
				data.setTitleId(rs.getInt("level"));
				data.setDailyGetIngots(rs.getInt("dailyIngots"));
				data.setPatronsAbilityAddition(rs.getInt("patronsStrengthAddition"));
				data.setNeedPrestige(new BigInteger(rs.getString("needPrestige")));
				data.setNeedEarnSpeed(new BigInteger(rs.getString("needEarnSpeed")));
				data.setReward(PropertyHelper.parseStringToProperty(rs.getString("rewards")));
				data.setChildTrainAddExp(rs.getInt("childTrainAddExp"));
				List<BigInteger> l = StringUtils.stringToBigIntegerList(rs.getString("childBaseEarnSpeed"), "\\|");
				data.setChildBaseEarnSpeed1(l.get(0));
				data.setChildBaseEarnSpeed2(l.get(1));
				data.setUnionDonateTimes(rs.getInt("guildFortificationsMaxNum"));
				data.setBeautyAddEnergy(rs.getInt("beautyAddTrystTimes"));
				data.setBeautyEnergyRestoreTimeLower(rs.getInt("beautyTrystRestoreTimeLower"));
				data.setChildAddTrainTimes(rs.getInt("childAddTrainTimes"));
				data.setMineBaseOutput(rs.getInt("mineWarBaseProduce"));
				data.setTravelPowerTimes(rs.getInt("travelPowerTimes"));
				data.setBanquetEffect(rs.getInt("banquetEffect"));
				data.setTraderWarEarnSpeed(rs.getInt("traderWarEarnSpeed"));
				data.setMaleReward(PropertyHelper.parseStringToProperty(rs.getString("maleAddRewards")));
				data.setFemaleReward(PropertyHelper.parseStringToProperty(rs.getString("remaleAddRewards")));
				data.setZsAddition(rs.getInt("zsAddition"));
				dataMap.put(data.getTitleId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}
}
